//
//  SelectNumberView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/27.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol SelectNumberDelegate:class {
    func changeSlider(value:Double)
}
class SelectNumberView: UIView {
    let btnTag = 1000
    let progressTag = 3000
    var nextBtn:UIView?
    var value:Double = 0.00 //滑动比例
    var firstView:UIView?
    weak var delegate:SelectNumberDelegate?
    var isBuyBool:Bool = true {
        didSet {
            if isBuyBool {
                currentColor = kGreenColor
            }else {
               currentColor = kRedColor
            }
            self.updateColor()
        }
    }
    var currentColor:UIColor = kGreenColor
    let kwidth:CGFloat = (kSCREEN_WIDTH*0.6-42)/4 //间距
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        for i in 0..<5 {
            let btn = UIView()
            btn.layer.cornerRadius = 5
            btn.layer.masksToBounds = true
            btn.tag = i+btnTag
            btn.backgroundColor = UIColor(hexStr: "#e6e8e8")
            self.addSubview(btn)
            if i == 0 {
                firstView = btn
                btn.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset(0)
                    make.centerY.equalToSuperview()
                    make.height.width.equalTo(12)
                }
                btn.backgroundColor = currentColor
                btn.layer.cornerRadius = 6
                nextBtn = btn
                continue
            }else {
                btn.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset( 0 + CGFloat(i)*kwidth)
                    make.centerY.equalToSuperview()
                    make.height.width.equalTo(10)
                }
                
            }
            let itmx = CGFloat(13) + CGFloat(i-1) * kwidth
            let itemWidth = kwidth-16.00
            let lineView = UIView(frame: CGRect(x: itmx, y: 18, width: itemWidth, height: 4))
            lineView.backgroundColor = UIColor(hexStr: "#e6e8e8")
            lineView.layer.cornerRadius = 2
            lineView.layer.masksToBounds = true
            self.addSubview(lineView)

            let progressView = UIView(frame: CGRect(x: itmx, y: 18, width: 0, height: 4))
            progressView.backgroundColor = currentColor
            progressView.layer.cornerRadius = 2
            progressView.tag = i + progressTag
            progressView.layer.masksToBounds = true
            self.addSubview(progressView)
            nextBtn = btn
        }
        
    }
    func updateColor() {
        firstView?.backgroundColor = currentColor
    }
  
    func showSlider(x:CGFloat) {
        //计算 value
        if kwidth*4 <= x {
            value = 1
        }else if x < 10{
            value = 0
        }else {
            value = Double(x / (kwidth*4))
        }
        if delegate != nil && ((delegate?.changeSlider) != nil) {
            delegate?.changeSlider(value: value)
        }
        
        if x < CGFloat(kwidth) {
            self.selectItem(index:0)
        }else if CGFloat(kwidth) <= x && x < CGFloat(kwidth*2) {
            self.selectItem(index:1)
        }else if CGFloat(kwidth*2) <= x &&  x < CGFloat(kwidth*3) {
            self.selectItem(index:2)
        }else if CGFloat(kwidth*3) <= x && x < CGFloat(kwidth*4) {
            self.selectItem(index:3)
        }else if CGFloat(kwidth*4) <= x && x < CGFloat(kSCREEN_WIDTH*0.6) {
            self.selectItem(index:4)
        }else {
            self.selectItem(index:5)
        }
        
        //修改对应的线长度
        if CGFloat(13) < x && x <  CGFloat(kwidth - 3) {
            let progressView = self.viewWithTag(progressTag+1)
            let itmx = CGFloat(13) + CGFloat(0) * kwidth
            progressView?.frame = CGRect(x: itmx, y: 18, width: x - CGFloat(13), height: 4)
        }else if CGFloat(13 + CGFloat(kwidth - 3)) < x && x < CGFloat(kwidth*2-3) {
            let progressView = self.viewWithTag(progressTag+2)
            let itmx = CGFloat(13) + CGFloat(1) * kwidth
            progressView?.frame = CGRect(x: itmx, y: 18, width: x - CGFloat(13 + kwidth), height: 4)
            
        }else if CGFloat(13 + CGFloat(kwidth*2 - 3)) < x && x < CGFloat(kwidth*3-3) {
            let progressView = self.viewWithTag(progressTag+3)
            let itmx = CGFloat(13) + CGFloat(2) * kwidth
            progressView?.frame = CGRect(x: itmx, y: 18, width: x - CGFloat(13 + kwidth)*2, height: 4)
            
        }else if CGFloat(13 + CGFloat(kwidth*3 - 3)) < x && x < CGFloat(kwidth*4-3) {
            let progressView = self.viewWithTag(progressTag+4)
            let itmx = CGFloat(13) + CGFloat(3) * kwidth
            progressView?.frame = CGRect(x: itmx, y: 18, width: x - CGFloat(13 + kwidth*3), height: 4)
        }
    }
    
    
    /// 修改显示的样式
    ///
    /// - Parameter index: index
    func selectItem(index:Int) {
        let tag = index
        for i in tag ..< 5  {
            let btn = self.viewWithTag(i+btnTag)
            btn?.backgroundColor = UIColor(hexStr: "#e6e8e8")
            btn?.snp.updateConstraints({ (make) in
                make.width.height.equalTo(10)
            })
            btn?.layer.cornerRadius = 5
            let line = self.viewWithTag(i+progressTag)
            line?.backgroundColor = currentColor
            let itmx = CGFloat(13) + CGFloat(i) * kwidth
            line?.frame = CGRect(x: itmx, y: 18, width: 0, height: 4)
        }
        for i in 0...tag {
            let btn = self.viewWithTag(i+btnTag)
            btn?.backgroundColor = currentColor
            btn?.snp.updateConstraints({ (make) in
                make.width.height.equalTo(12)
            })
            btn?.layer.cornerRadius = 6
            let line = self.viewWithTag(i+progressTag)
            line?.backgroundColor = currentColor
            let itmx = CGFloat(13) + CGFloat(i-1) * kwidth
            line?.frame = CGRect(x: itmx, y: 18, width: kwidth-16, height: 4)
        }
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
extension SelectNumberView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let pt  = touch?.location(in: self)
        self.showSlider(x: (pt?.x)!)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let pt  = touch?.location(in: self)
        self.showSlider(x: (pt?.x)!)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let pt  = touch?.location(in: self)
        self.showSlider(x: (pt?.x)!)
    }
    
}
